package com.qiyou.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qiyou.dao.IActivityDao;
import com.qiyou.dao.IBlogDao;
import com.qiyou.dao.IScenicSpotDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.Activity;
import com.qiyou.entity.Blog;
import com.qiyou.entity.ScenicSpot;

/**
 * 開始頁面,負責对主页的初始化
 * 
 * @author 赛艳志
 */
@WebServlet("/Index")
public class IndexServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        
        // 数据库连接
        IActivityDao activityDao = DaoFactory.getActivityDao();
        IBlogDao blogDao = DaoFactory.getBlogDao();
        IScenicSpotDao spotDao = DaoFactory.getScenicSpotDao();
        
        // 查询sql语句
        String sqlinvt = "order by acDate desc limit 4";
        String sqlblog = "order by blogDate desc limit 4";
        String sqlsspt = "order by scsNums desc limit 4";

        List<Activity> recentInvit = null;
        List<Blog> recentBlog = null;
        List<ScenicSpot> recentSpot = null;

        try {
            recentInvit = activityDao
                    .getActivities(new Object[] { sqlinvt });
            recentBlog = blogDao.getBlogs(new Object[] { sqlblog });
            recentSpot = spotDao.getScenicSpots(new Object[] { sqlsspt });
        } catch (SQLException e) {
        }

        request.getSession().setAttribute("recentInvit", recentInvit);
        request.getSession().setAttribute("recentBlog", recentBlog);
        request.getSession().setAttribute("recentSpot", recentSpot);

        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
